//
//  DebugWindow.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/11.
//  Copyright © 2021 edward. All rights reserved.
//

#import "DebugWindow.h"
#import "UIImage+DebugManager.h"



@interface DebugWindow ()

///
@property(nonatomic,strong) UIButton *button;

@end

@implementation DebugWindow

int const debugWindowTag = 9998;

- (instancetype)initWithFrame:(CGRect)frame andClickBlock:(void(^)(void))clickBlock
{
    self = [self initWithFrame:frame];
    if (self) {
        self.clickDebugWindow = clickBlock;
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        #if defined(__IPHONE_13_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
            if (@available(iOS 13.0, *)) {
                UIScene *scene = [[UIApplication sharedApplication].connectedScenes anyObject];
                if (scene) {
                    self.windowScene = (UIWindowScene *)scene;
                }
            }
        #endif
        self.backgroundColor = [UIColor clearColor];
        self.tag = debugWindowTag;
        self.layer.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor;
        self.layer.shadowOffset = CGSizeMake(2.5, 2.5);
        self.layer.shadowRadius = 5;
        self.layer.shadowOpacity = 0.5;
        
        self.windowLevel = UIWindowLevelStatusBar + 50.0f;
        if (!self.rootViewController) {
            self.rootViewController = [[UIViewController alloc] init];
        }
        [self.rootViewController.view addSubview:self.button];
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(locationChange:)];
        pan.delaysTouchesBegan = YES;
        [self addGestureRecognizer:pan];
        
    }
    return self;
}
- (void)show
{
    self.hidden = NO;
}
-(void)locationChange:(UIPanGestureRecognizer*)pan
{
    UIView *panView = self;
    CGPoint tranp = [pan translationInView:panView];
    CGFloat safeBottom = 0;
    if (@available(iOS 11.0, *)) {
       safeBottom = self.safeAreaInsets.bottom;
    }
    //重新设置拖动的起始点相应为上一次的拖动后的点，即每改变一次均要重置定位，类似的拖动，放大，旋转均要重置拖动后的起始点
    [pan setTranslation:CGPointZero inView:panView];
    CGFloat newX = panView.center.x + tranp.x;
    CGFloat newY = panView.center.y + tranp.y;
    if (newX < UIScreen.mainScreen.bounds.size.width / 2) {

        newX = self.bounds.size.width/2;
    }else
    {
        newX = UIScreen.mainScreen.bounds.size.width - (self.bounds.size.width/2);
    }
    #pragma mark -----------🔽------------
    if (newY > CGRectGetMaxY([UIScreen mainScreen].bounds)-safeBottom - (self.bounds.size.width/2)) {
        newY = CGRectGetMaxY([UIScreen mainScreen].bounds)-safeBottom - (self.bounds.size.width/2);
    }
    if (newY < [UIApplication sharedApplication].statusBarFrame.size.height + (self.bounds.size.width/2)) {
        newY = [UIApplication sharedApplication].statusBarFrame.size.height + (self.bounds.size.width/2);
    }
//    CGFloat centerY = MAX(MIN(newY, CGRectGetMaxY([UIScreen mainScreen].bounds)-safeBottom), [UIApplication sharedApplication].statusBarFrame.size.height);
    /**                   注释代码与maxminy代码效果一致                      */
    #pragma mark -----------🔼------------
    
    if(pan.state == UIGestureRecognizerStateChanged)
    {
        self.center = CGPointMake(panView.center.x + tranp.x, panView.center.y + tranp.y);
    }else if (pan.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.center = CGPointMake(newX, newY);
        }];
    }
    
}
- (UIButton *)button
{
    if(!_button)
    {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = self.bounds;
        [_button setImage:[UIImage debugmanager_imageNamed:@"ic_out_window"] forState:UIControlStateNormal];
        _button.layer.cornerRadius = self.bounds.size.height/2;
        _button.clipsToBounds = YES;
        
        
        [_button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}
- (void)clickButton:(UIButton *)sender
{
    if (self.clickDebugWindow) {
        self.clickDebugWindow();
    }
}
@end
